package com.gjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjy.domain.Department;
import com.gjy.domain.Employee;
import com.gjy.domain.User;
import com.gjy.service.DepartmentService;
import com.gjy.service.EmployeeService;
import com.gjy.service.UserService;
import com.gjy.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宫静雨
 * @since 2021-03-15
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/updateOneEmp",method = RequestMethod.POST)
    public String updateOneEmp(@RequestParam String id,
                               @RequestParam(value = "name",required = false) String name,
                               @RequestParam(value = "salary",required = false) String salary,
                               @RequestParam(value = "department",required = false) String department){
        Employee employee = new Employee();
        employee.setId(Integer.parseInt(id));
        employee.setName(name);
        employee.setSalary(salary);
        employee.setDepartment(department);
        employeeService.updateById(employee);
        return "admin";
    }

    @RequestMapping(value = "/delOneEmp",method = RequestMethod.POST)
    public String delOneEmp(@RequestParam String id,
                            @RequestParam String name){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("name",name);
        employeeService.remove(queryWrapper);
        return "admin";
    }

    @ResponseBody
    @RequestMapping("/depts")
    public Msg getAllDept(){
        List<Department> list = departmentService.list();
        return Msg.success().add("depts",list);
    }

    @RequestMapping(value = "/saveOneEmp",method = RequestMethod.POST)
    public String saveOneEmp(@RequestParam String name,
                             @RequestParam String gender,
                             @RequestParam String entry,
                             @RequestParam String salary,
                             @RequestParam String department){
        Employee employee = new Employee();
        String[] split = entry.split("-");
        employee.setId(null);
        employee.setName(name);
        employee.setGender(gender);
        LocalDate localDate = LocalDate.of(Integer.parseInt(split[0]),Integer.parseInt(split[1]),
                Integer.parseInt(split[2]));
        employee.setEntry(localDate);
        employee.setSalary(salary);
        employee.setDepartment(department);
        employeeService.save(employee);
        return "admin";
    }

    @ResponseBody
    @RequestMapping("/getAllEmployee")
    public Msg getAllEmployee(@RequestParam("current") Integer current){
        PageHelper.startPage(current,10);
        List<Employee> list = employeeService.list();
        PageInfo<Employee> pageInfo = new PageInfo<>(list,5);
        return Msg.success().add("employees",pageInfo);
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam("name") String name,
                      @RequestParam("password") String password){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("password",password);
        User user = userService.getOne(queryWrapper);
        if (user != null)
            return "admin";
        else
            return "error";
    }
}

